import G from "../Utilities/Global";
import MapAssetStruct from "../Structs/MapAssetStruct";

/**
 * 地图物件配表信息
 * created by Five on 2018-11-6 14:30:49
 */

export default class MapAssetConfig {

    public Assets: Map<Number, MapAssetStruct>;
    public AssetsArr: Array<MapAssetStruct>;

    constructor() {
        this.Assets = new Map<Number, MapAssetStruct>();
        this.AssetsArr = new Array<MapAssetStruct>();
        this.init();        
    }

    private async init() {
        let asset = await G().Res.LoadFile("config/tables", "MapAssets") as cc.JsonAsset;
        let array = asset.json as Array<any>;
        array.forEach(struct => {
            // 外部使用时,注意实例化
            let mas = struct as MapAssetStruct
            this.Assets.set(mas.id, mas);
            this.AssetsArr.push(mas);
        })
        cc.log(this.Assets)
    }

    public GetAssetByType(type: number): MapAssetStruct {
        let asset = this.AssetsArr.find(x => (x.type & type) > 0);
        if(asset)
            return new MapAssetStruct(asset);
        return null;
    }

    public GetAsset(id: number): MapAssetStruct {
        let asset = this.Assets.get(id);
        if(asset)
            return new MapAssetStruct(asset);
        return null;
    }

    public GetAssetConfig(id: number): MapAssetStruct {
        return this.Assets.get(id);
    }
}